//
//  TJResponderTextView.m
//  XWC
//
//  Created by Hello on 2022/10/24.
//

#import "HJResponderTextView.h"
#import "NSString+emoji.h"
#import "QMUICommonDefines.h"


const UIEdgeInsets textViewFixTextInsets = {0, 5, 0, 5};

@interface HJResponderTextView()

@property(nonatomic, assign) UIEdgeInsets placeholderMargins;

@end


@implementation HJResponderTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        

        self.placeholderMargins = UIEdgeInsetsZero;
        self.placeholderLabel = [[UILabel alloc] init];
        self.placeholderLabel.font = UIFontMake(14);
        self.placeholderLabel.numberOfLines = 0;
        self.placeholderLabel.alpha = 0;
        [self addSubview:self.placeholderLabel];
        
    }
    return self;
}
 
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    
    [self updatePlaceholderLabelHidden];
    
    if (!placeholder) return;
    self.placeholderLabel.attributedText = [[NSAttributedString alloc] initWithString:_placeholder attributes:self.typingAttributes];
    self.placeholderLabel.textColor = UIColor.tj_placeholderTextColor;
    [self sendSubviewToBack:self.placeholderLabel];
    [self setNeedsLayout];
}

- (void)updatePlaceholderLabelHidden {
    if (self.text.length == 0 && self.placeholder.length > 0) {
        self.placeholderLabel.alpha = 1;
    } else {
        self.placeholderLabel.alpha = 0;// 用alpha来让placeholder隐藏，从而尽量避免因为显隐 placeholder 导致 layout
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.placeholder.length > 0) {
        UIEdgeInsets labelMargins = UIEdgeInsetsConcat(UIEdgeInsetsConcat(self.textContainerInset, self.placeholderMargins), textViewFixTextInsets);
        CGFloat limitWidth = CGRectGetWidth(self.bounds) - UIEdgeInsetsGetHorizontalValue(self.contentInset) - UIEdgeInsetsGetHorizontalValue(labelMargins);
        CGFloat limitHeight = CGRectGetHeight(self.bounds) - UIEdgeInsetsGetVerticalValue(self.contentInset) - UIEdgeInsetsGetVerticalValue(labelMargins);
        CGSize labelSize = [self.placeholderLabel sizeThatFits:CGSizeMake(limitWidth, limitHeight)];
        labelSize.height = fmin(limitHeight, labelSize.height);
        self.placeholderLabel.frame = CGRectFlatMake(labelMargins.left, labelMargins.top, limitWidth, labelSize.height);
    }
}


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self updatePlaceholderLabelHidden];
}

- (UIResponder *)nextResponder
{
    if(_overrideNextResponder == nil){
        return [super nextResponder];
    }
    else{
        return _overrideNextResponder;
    }
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (_overrideNextResponder != nil)
        return NO;
    else
        return [super canPerformAction:action withSender:sender];
}

- (void)deleteBackward
{
    id<TJResponderTextViewDelegate> delegate = (id<TJResponderTextViewDelegate>)self.delegate;
    
    if ([delegate respondsToSelector:@selector(onDeleteBackward:)]) {
        [delegate onDeleteBackward:self];
    }
    
    [super deleteBackward];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:nil];
    if (self.delegate && [self.delegate respondsToSelector:@selector(textViewDidChange:)]) {
        [self.delegate textViewDidChange:self];
    }
}

- (void)copy:(__unused id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = [[self.textStorage attributedSubstringFromRange:self.selectedRange] getPlainString];
}

- (void)cut:(nullable id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = [[self.textStorage attributedSubstringFromRange:self.selectedRange] getPlainString];
    UIFont *textFont = UIFontMake(14);
    NSAttributedString *spaceString = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName: textFont}];
    [self.textStorage replaceCharactersInRange:self.selectedRange withAttributedString:spaceString];
    
}
 
@end
